package com.cwj.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author chenwujie
 * @date 2020-12-30 15:04
 */
public class JDKProxy {
    private Object target;

    public JDKProxy(Object target) {
        this.target = target;
    }

    public Object getProxyInstance(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    System.out.println("前");
                    Object invoke = method.invoke(target, args);
                    System.out.println(invoke);
                    System.out.println("后");
                    return invoke;
                }
        );
    }

    public static void main(String[] args) {
        Hello hello = new HelloImpl();
        JDKProxy jdkProxy = new JDKProxy(hello);
        Hello proxyInstance = (Hello)jdkProxy.getProxyInstance();
        proxyInstance.hello();
    }
}
